﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace miniQQ_CSharp_SDK
{
    public class AddOn
    {
        /// <summary>
        /// 返回[@QQ号]
        /// </summary>
        /// <param name="TigObjF">At对象</param>
        /// <returns></returns>
        public static string AtQQ(string TigObjF)
        {
            return "[@" + TigObjF + "]";
        }


        /// <summary>
        /// 判断是否@了机器人
        /// </summary>
        /// <param name="RobotQQ">机器人QQ</param>
        /// <param name="Msg">判断文本</param>
        /// <returns></returns>
        public static bool IsAtRobot(string RobotQQ, string Msg)
        {
            {
                return Msg.Contains("@" + RobotQQ) || Msg.Contains("@" + Api.ApiGetNick(RobotQQ, RobotQQ));
            }
        }

        /// <summary>
        /// 判断targetQQ是否是管理员
        /// </summary>
        /// <param name="robotQQ">机器人QQ</param>
        /// <param name="groupNum">群号</param>
        /// <param name="targetQQ">目标QQ</param>
        /// <returns></returns>
        public static bool isAdmin(string robotQQ, string groupNum, string targetQQ)
        {
            if (Api.ApiGetAdminList(robotQQ, groupNum).Contains(targetQQ))
            {
                return true;
            }
            else return false;
        }


        private static string Get(string url)
        {
            string all = string.Empty;
            //发送请求
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "get";//设置方法
            request.ContentType = "text/html;charset=UTF-8";
            //获得响应
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //读取流
            Stream stream = response.GetResponseStream();
            StreamReader sr = new StreamReader(stream, Encoding.UTF8);
            all = sr.ReadToEnd();
            //关闭流
            stream.Close();
            sr.Close();
            return all;
        }
        public static string Post(string url, Dictionary<string, string> dic)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json";
            #region 添加Post 参数
            StringBuilder builder = new StringBuilder();
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }
            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

    }


}
